<template>
	<view>
		<view class="nologin" v-if="!userInfo._openid">
			<button type="primary" open-type="getUserInfo" @getuserinfo="handleWechatLogin">
				登录
			</button>
		</view>

		<view class="user" v-else>
			<view class="userinfo">
				<!-- 用户头像 -->
				<view class="user-img">
					<image :src="userInfo.avatarUrl"></image>
				</view>
				<!-- 用户昵称 -->
				<view class="username">{{ userInfo.nickName }}</view>
			</view>
			<!-- 控制是否匹配的开关 -->
			<view class="state-switch" v-if="userInfo.userState">
				<view>{{
					userInfo.userState === '0' ? '匹配关闭' : userInfo.userState === '1' ? '匹配中' : ''
				}}</view>
				<view
					><switch
						@change="handleStateChange"
						:checked="userInfo.userState === '0' ? false : userInfo.userState === '1' ? true : ''"
				/></view>
			</view>

			<!-- 匹配信息 -->
			<view class="state-switch" v-if="userInfo.userState" @click="toResultPage">
				<view>匹配信息</view>
				<view class="iconfont .icon-right"></view>
			</view>
			<view class="logout" @click="logout">
				退出登录
			</view>
		</view>
	</view>
</template>

<script>
	import { mapState } from 'vuex'

	export default {
		data() {
			return {
				// 用户是否在匹配中的状态,默认为2:不参与匹配,0:匹配中,1表示匹配成功,不在匹配中
				userState: '2',
			}
		},
		computed: {
			...mapState(['userInfo']),
		},
		onLoad() {
			if (this.userInfo._openid) {
				this.$http({
					name: 'get_user_info_byid',
					data: {
						_openid: this.userInfo._openid,
					},
				}).then((res) => {
					if (res.code === '200') {
						this.userInfo.userState = res.data[0].userState
					}
				})
			}
		},
		methods: {
			// 去匹配结果页面
			toResultPage() {
				uni.navigateTo({
					url: '/pages/result/result',
				})
			},
			// 监听switch开关改变事件
			handleStateChange(e) {
				if (e.detail.value) {
					this.$http({
						name: 'update_sky_user_ById',
						data: {
							_openid: this.userInfo._openid,
							userState: '1',
						},
					}).then((res) => {
						if (res.stats.updated === 1) {
							this.userInfo.userState = '1'
							this.$store.dispatch('setUserInfo', this.userInfo)
							this.$store.dispatch('setResultInfo', [])
						}
					})
				} else {
					this.$http({
						name: 'update_sky_user_ById',
						data: {
							_openid: this.userInfo._openid,
							userState: '0',
						},
					}).then((res) => {
						if (res.stats.updated === 1) {
							this.userInfo.userState = '0'
							this.$store.dispatch('setUserInfo', this.userInfo)
						}
					})
				}
			},
			// 微信登录
			handleWechatLogin(e) {
				// 获取用户信息
				let usertemp = e.detail.userInfo
				if (!usertemp) {
					uni.showToast({
						title: '登录失败',
						icon: 'none',
						duration: 2000,
					})
					return
				}
				// 登录
				this.$http({
					name: 'sky_login',
				}).then((res) => {
					if (res.state === '200') {
						// 存在此用户,登录成功
						uni.showToast({
							title: '登录成功',
							icon: 'success',
							duration: 2000,
						})
						this.$store.dispatch('setUserInfo', res.data[0])
					} else {
						let user = {}
						// 用户不存在,封装用户信息,保存到数据库
						user._openid = res.openid
						// 是否成年
						user.adult = '是'
						user.cardNum = 1
						user.phone = ''
						user.qq = ''
						user.wechat = ''
						// 拼卡状态,0:买卡,1:卖卡,2:互卡
						user.pinkaState = '0'
						user.season = ''
						user.server = ''
						// 匹配状态: 0:关闭匹配,1:开始匹配
						user.userState = '0'
						user.pinkaUserId = []
						// 用户微信昵称
						user.nickName = usertemp.nickName
						user.avatarUrl = usertemp.avatarUrl
						// 用户游戏内昵称
						user.gameNickName = ''
						this.$http({
							name: 'add_sky_user',
							data: user,
						}).then((res) => {
							uni.showToast({
								title: '登录成功',
								icon: 'success',
								duration: 2000,
							})
							this.$store.dispatch('setUserInfo', user)
						})
					}
				})
			},
			// 退出登录
			logout() {
				uni.showModal({
					content: '是否退出登录',
					confirmColor: '#c0392b',
					success: (res) => {
						if (res.confirm) {
							this.$store.dispatch('setUserInfo', {})
						}
					},
				})
			},
		},
	}
</script>
<style lang="scss" scoped>
	.nologin {
		height: 250rpx;
		background-color: $brand-color;
		display: flex;
		justify-content: center;
		align-items: center;
	}
	.user {
		box-sizing: border-box;
		.userinfo {
			padding: 0 30rpx;
			height: 250rpx;
			background-color: $brand-color;
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;
			.user-img {
				width: 150rpx;
				height: 150rpx;
				image {
					width: 100%;
					height: 100%;
				}
			}
			.username {
				color: #fff;
				margin: 20rpx 0;
			}
		}
		.state-switch {
			width: 750rpx;
			height: 100rpx;
			background-color: #fff;
			line-height: 100rpx;
			margin: 20rpx 0;
			padding: 0 30rpx;
			display: flex;
			justify-content: space-between;
			box-sizing: border-box;
			.icon-right {
				color: #bdc3c7;
				font-size: 40rpx;
			}
		}
		.logout {
			width: 750rpx;
			height: 100rpx;
			background-color: #fff;
			line-height: 100rpx;
			text-align: center;
			margin: 20rpx 0;
		}
	}
</style>
